package com.sven.sharedb.range.db;

import com.sven.sharedb.range.mapper.ShardingOrderWzMapper;
import com.sven.sharedb.core.db.service.abstracts.AbstractCountCreateDateShareJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author sven.wang
 * @version 2022/5/26 11:55
 */
@Slf4j
@Service("shardingOrderWzShareJob")
@RefreshScope
public class ShardingOrderWzShareJobComponent extends AbstractCountCreateDateShareJob {

    /**
     * 表数据量阈值
     */
    @Value("${datasource.sharding-table.sharding_order_wz.count-threshold:}")
    private Integer shardingOrderWzCountThreshold;
    /**
     * 分表实际节点
     */
    @Value("${datasource.sharding-table.sharding_order_wz.actual-data-nodes:}")
    private String shardingOrderWzCountActualDataNodes;
    /**
     * 逻辑表名
     */
    private final static String LOGIC_TABLE_NAME = "sharding_order_wz";

    @Resource
    private ShardingOrderWzMapper shardingOrderWzMapper;

    /**
     * 获取逻辑表名
     * @return 逻辑表名
     */
    @Override
    public String getLogicTableName() {
        return LOGIC_TABLE_NAME;
    }

    /**
     * 获取表数据量阈值
     * @return 表数据量阈值
     */
    @Override
    public Integer getThreshold() {
        return shardingOrderWzCountThreshold;
    }

    /**
     * 获取分表实际节点
     * @return 分表实际节点
     */
    @Override
    public String getActualDataNodes() {
        return shardingOrderWzCountActualDataNodes;
    }

    /**
     * 创建新时段表sql
     * @param newTableName 新表名
     */
    @Override
    public void createNextTimeTable(String newTableName) {
        int result = shardingOrderWzMapper.createNextTimeTable(newTableName, LOGIC_TABLE_NAME);
        log.info("createNextTimeTable:{}",result);
    }

    /**
     * 计算表数量sql
     * @param tableName 表名
     * @return 表数量
     */
    @Override
    public int getTableNameCount(String tableName) {
        return shardingOrderWzMapper.selectTableNameCount(tableName);
    }
}
